﻿using SqlSugar;

namespace SBlogCore.Common.DatabaseAccessor.Repositorys.SQLSuger
{
    public partial class SqlSugerRepository<T> : ISqlSugerRepository<T> where T : class, new()
    {
        /// <summary>
        /// 更新实体数据
        /// </summary>
        /// <param name="entity">博文实体类</param>
        /// <returns></returns>
        public async Task<bool> Update(T entity)
        {
            //这种方式会以主键为条件
            return await _db.Updateable(entity).ExecuteCommandHasChangeAsync();
        }

        public async Task<bool> Update(T entity, string where)
        {
            return await _db.Updateable(entity).Where(where).ExecuteCommandHasChangeAsync();
        }

        public async Task<bool> Update(string sql, SugarParameter[] parameters = null)
        {
            return await _db.Ado.ExecuteCommandAsync(sql, parameters) > 0;
        }

        public async Task<bool> Update(object operateAnonymousObjects)
        {
            return await _db.Updateable<T>(operateAnonymousObjects).ExecuteCommandAsync() > 0;
        }

        public async Task<bool> Update(
          T entity,
          List<string> lstColumns = null,
          List<string> lstIgnoreColumns = null,
          string where = ""
            )
        {
            IUpdateable<T> up = _db.Updateable(entity);
            if (lstIgnoreColumns != null && lstIgnoreColumns.Count > 0)
            {
                up = up.IgnoreColumns(lstIgnoreColumns.ToArray());
            }
            if (lstColumns != null && lstColumns.Count > 0)
            {
                up = up.UpdateColumns(lstColumns.ToArray());
            }
            if (!string.IsNullOrEmpty(where))
            {
                up = up.Where(where);
            }
            return await up.ExecuteCommandHasChangeAsync();
        }
    }
}
